package com.tencent.mm.opensdk.diffdev.a;

import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.tencent.mm.opensdk.diffdev.IDiffDevOAuth;
import com.tencent.mm.opensdk.diffdev.OAuthListener;
import java.util.ArrayList;
import java.util.List;

public final class a implements IDiffDevOAuth {
    private List h;
    private Handler handler;
    private d i;
    private OAuthListener j;

    public a() {

        this.handler = null;
        this.h = new ArrayList();
        this.j = new b(this);
    }

    static List a(a arg1) {
        return arg1.h;
    }

    public final void addListener(OAuthListener arg2) {
        if(!this.h.contains(arg2)) {
            this.h.add(arg2);
        }
    }

    public final boolean auth(String arg10, String arg11, String arg12, String arg13, String arg14, OAuthListener arg15) {
        boolean v0;
        Log.i("MicroMsg.SDK.DiffDevOAuth", "start auth, appId = " + arg10);
        if(arg10 == null || arg10.length() <= 0 || arg11 == null || arg11.length() <= 0) {
            Log.d("MicroMsg.SDK.DiffDevOAuth", String.format("auth fail, invalid argument, appId = %s, scope = %s", arg10, arg11));
            v0 = false;
        }
        else {
            if(this.handler == null) {
                this.handler = new Handler(Looper.getMainLooper());
            }

            this.addListener(arg15);
            if(this.i != null) {
                Log.d("MicroMsg.SDK.DiffDevOAuth", "auth, already running, no need to start auth again");
                v0 = true;
                return v0;
            }

            this.i = new d(arg10, arg11, arg12, arg13, arg14, this.j);
            d v0_1 = this.i;
            if(Build.VERSION.SDK_INT >= 11) {
                v0_1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,new Object[0]);
            }
            else {
                v0_1.execute(new Object[0]);
            }

            v0 = true;
        }

        return v0;
    }

    static Handler b(a arg1) {
        return arg1.handler;
    }

    static d c(a arg1) {
        arg1.i = null;
        return null;
    }

    public final void detach() {
        Log.i("MicroMsg.SDK.DiffDevOAuth", "detach");
        this.h.clear();
        this.stopAuth();
    }

    public final void removeAllListeners() {
        this.h.clear();
    }

    public final void removeListener(OAuthListener arg2) {
        this.h.remove(arg2);
    }

    public final boolean stopAuth() {
        Log.i("MicroMsg.SDK.DiffDevOAuth", "stopAuth");
        try {
            if(this.i != null) {
                boolean v0_1;
                try {
                    v0_1 = this.i.a();
                } catch(Exception v0) {
                    Log.w("MicroMsg.SDK.DiffDevOAuth", "stopAuth fail, ex = " + v0.getMessage());
                    v0_1 = false;
                }
                this.i = null;
                return v0_1;
            }
        } catch(Exception v0) {
        }

        boolean v0_1 = true;
        this.i = null;
        return v0_1;

    }
}

